////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Menu                                       //
////////////////////////////////////////////////

function dhlite.MenuCall_ReloadFromCloud()
	if dhlite_cloud then
		dhlite_cloud:Ask()
	end
	
end

function dhlite.MenuCall_ReloadFromLocale()
	if dhlite_cloud then
		dhlite_cloud:LoadLocale()
	end
	
end

function dhlite.BuildMenu( opt_tExpand )
	if dhlite.DermaPanel then dhlite.DermaPanel:Remove() end
	
	local bCanGetVersion = dhlite_internal ~= nil
	local MY_VERSION, ONLINE_VERSION, DOWNLOAD_LINK
	local ONLINE_VERSION_READ = -1
	if bCanGetVersion then
		MY_VERSION, ONLINE_VERSION, DOWNLOAD_LINK = dhlite_internal.GetVersionData()
		
		if ONLINE_VERSION == -1 then
			ONLINE_VERSION_READ = "<offline>"
		else
			ONLINE_VERSION_READ = tostring( ONLINE_VERSION )
		end
		
	end
	
	dhlite.DermaPanel = dhlite.Util_MakeFrame( 280, ScrH() * 0.80 )
	local refPanel = dhlite.DermaPanel
	
	dhlite.Util_MakeCategory( refPanel, "General", 1 )
	dhlite.Util_AppendCheckBox( refPanel, "Enable" , "dhlite_core_enable" )
	
	--Helper label
	do
		local GeneralTextLabelMessage = "The command \"dhlite_menu\" calls this menu.\n"
		GeneralTextLabelMessage = GeneralTextLabelMessage .. "Example : To assign " .. DHLITE_NAME .. " menu to F10, type in the console :"
		
		dhlite.Util_AppendLabel( refPanel, GeneralTextLabelMessage, 50, true )
		
		local GeneralCommandLabel = vgui.Create("DTextEntry")
		GeneralCommandLabel:SetText( "bind \"F10\" \"dhlite_menu\"" )
		GeneralCommandLabel:SetEditable( false )

		dhlite.Util_AppendPanel( refPanel, GeneralCommandLabel )
		
	end
	
	
	--Update label
	do
		if bCanGetVersion and (MY_VERSION and ONLINE_VERSION and (MY_VERSION < ONLINE_VERSION)) then
			GeneralTextLabelMessage = "Your version is "..MY_VERSION.." and the updated one is "..ONLINE_VERSION.." ! You should update !"
			dhlite.Util_AppendLabel( refPanel, GeneralTextLabelMessage, 50, true )
			
			local CReload = vgui.Create("DButton")
			CReload:SetText( "Open full Changelog" )
			CReload.DoClick = dhlite.ShowChangelog
			dhlite.Util_AppendPanel( refPanel, CReload )
			
			dhlite.Util_AppendLabel( refPanel, "" )
			
			if ONLINE_VERSION and ONLINE_VERSION ~= -1 then
				local myVer = MY_VERSION or 0
				
				local contents = dhlite_internal.GetReplicate() or ( tostring( MY_VERSION or 0 ) .. "\n<Nothing to show>" )
				local split = string.Explode( "\n", contents )
				if (#split % 2) == 0 then
					local dList = vgui.Create("DListView")
					dList:SetMultiSelect( false )
					dList:SetTall( 150 )
					dList:AddColumn( "Ver." ):SetMaxWidth( 45 ) -- Add column
					dList:AddColumn( "Log" )
					
					local gotMyVer = false
					local i = 1
					while (i <= #split) and not gotMyVer do
						local iVer = tonumber( split[i] or 0 ) or 0
						if not gotMyVer and iVer ~= 0 and iVer <= myVer and (split[i+2] ~= "&") then
							dList:AddLine( "*" .. myVer .. "*", "< Locale version >" )
							gotMyVer = true
							
						else
							local myLine = dList:AddLine( (split[i] ~= "&") and split[i] or "", split[i+1] or "" )
							myLine:SizeToContents()
							
						end
						
						i = i + 2
						
					end
					
					dhlite.Util_AppendPanel( refPanel, dList )
					
				end
				
			end
			
		end
		
	end
	
	-- Style
	dhlite.Util_MakeCategory( refPanel, "Screenie", 0 )
	dhlite.Util_AppendSlider( refPanel, "Isometric < > Perspective", "dhlite_regmod_fov", 2, 100, 0)
	
	
	-- UI Design
	dhlite.Util_MakeCategory( refPanel, "UI Design", 0 )
	dhlite.Util_AppendLabel( refPanel, "Main color" )
	dhlite.Util_AppendColor( refPanel, "dhlite_uidesign_maincolor")
	dhlite.Util_AppendLabel( refPanel, "Background color" )
	dhlite.Util_AppendColor( refPanel, "dhlite_uidesign_backcolor")
	
	-- Elements
	dhlite.Util_MakeCategory( refPanel, "Elements", 1 )
	do
		local cat = dhlite.Util_CatchCurrentCategory( refPanel )
		cat.List:SetSpacing( 5 )
		
	end
	
	do
		local elements = dhlite:GetAllElements()
		for _,sName in pairs( dhlite:GetElementOrderTable() ) do
			local objElement = elements[ sName ]
			
			local category = vgui.Create("DhliteCollapsibleCheckbox", refPanel)
			category:SetExpanded( false )
			category:SetText( objElement:GetDisplayName() )
			category:SetConVar( "dhlite_elements_enable_" .. sName )
			
			category.List  = vgui.Create("DPanelList", category )
			category.List:EnableHorizontal( false )
			category.List:EnableVerticalScrollbar( false )
			
			category:SetContents( category.List )
			
			dhlite.Util_AppendPanel( refPanel, category )
			
		end
		
	end
	
	
	
	if dhlite_internal.IsUsingCloud then
		if dhlite_internal.IsUsingCloud() then
			dhlite.Util_MakeCategory( refPanel, "Using Cloud" .. (bCanGetVersion and (" [ v" .. tostring(MY_VERSION) .. " >> v" .. tostring(ONLINE_VERSION_READ) .. " ]") or " Version" ), 0 )
		
		else
			dhlite.Util_MakeCategory( refPanel, "Using Locale" .. (bCanGetVersion and (" [ v" .. tostring(MY_VERSION) .. " >> v" .. tostring(ONLINE_VERSION_READ) .. " ]") or " Version" ), 0 )
			
		end
		
	else
		dhlite.Util_MakeCategory( refPanel, "Cloud" .. (bCanGetVersion and (" [ v" .. tostring(MY_VERSION) .. " >> v" .. tostring(ONLINE_VERSION_READ) .. " ]") or " Version" ), 0 )
		
	end
	
	-- Reload from Cloud Button
	do
		local CReload = vgui.Create("DButton")
		CReload:SetText( "Reload from Cloud" )
		CReload.DoClick = dhlite.MenuCall_ReloadFromCloud
		dhlite.Util_AppendPanel( refPanel, CReload )
	end
	
	-- Reload from Locale Button
	if dhlite_internal then
		local CReload = vgui.Create("DButton")
		CReload:SetText( "Reload from Locale" )
		CReload.DoClick = dhlite.MenuCall_ReloadFromLocale
		dhlite.Util_AppendPanel( refPanel, CReload )
	end
	
	-- Changelog Button
	if dhlite_internal and dhlite_internal.GetReplicate then
		dhlite.Util_AppendLabel( refPanel, "" )
		
		local CChangelog = vgui.Create("DButton")
		CChangelog:SetText( "Open Changelog" )
		CChangelog.DoClick = dhlite.ShowChangelog
		dhlite.Util_AppendPanel( refPanel, CChangelog )
	end
	
	dhlite.Util_ApplyCategories( refPanel )
	
end


function dhlite.ShowMenuNoOverride( )
	dhlite.ShowMenu( true )
end

function dhlite.ShowMenu( optbKeyboardShouldNotOverride )
	if not dhlite.DermaPanel then
		dhlite.BuildMenu()
	end
	--dhlite.DermaPanel:Center()
	dhlite.DermaPanel:MakePopup()
	dhlite.DermaPanel:SetKeyboardInputEnabled( not optbKeyboardShouldNotOverride )
	dhlite.DermaPanel:SetVisible( true )
end

function dhlite.HideMenu()
	if not dhlite.DermaPanel then
		return
	end
	dhlite.DermaPanel:SetVisible( false )
end

function dhlite.DestroyMenu()
	if dhlite.DermaPanel then
		dhlite.DermaPanel:Remove()
		dhlite.DermaPanel = nil
	end
end

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//// SANDBOX PANEL .

function dhlite.Panel(Panel)	
	Panel:AddControl("Checkbox", {
			Label = "Enable", 
			Description = "Enable", 
			Command = "dhlite_core_enable" 
		}
	)
	Panel:AddControl("Button", {
			Label = "Open Menu (dhlite_menu)", 
			Description = "Open Menu (dhlite_menu)", 
			Command = "dhlite_menu"
		}
	)
	
	Panel:Help("To trigger the menu in any gamemode, type dhlite_menu in the console, or bind this command to any key.")
end

function dhlite.AddPanel()
	spawnmenu.AddToolMenuOption("Options", "Player", DHLITE_NAME, DHLITE_NAME, "", "", dhlite.Panel, {SwitchConVar = 'dhlite_core_enable'})
	
end

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// MOUNT FCTS.

function dhlite.MountMenu()
	concommand.Add( "dhlite_menu", dhlite.ShowMenuNoOverride )
	concommand.Add( "dhlite_call_menu", dhlite.ShowMenuNoOverride )
	concommand.Add( "+dhlite_menu", dhlite.ShowMenu )
	concommand.Add( "-dhlite_menu", dhlite.HideMenu )
	
end

function dhlite.UnmountMenu()
	dhlite.DestroyMenu()

	concommand.Remove( "dhlite_call_menu" )
	concommand.Remove( "dhlite_menu" )
	concommand.Remove( "+dhlite_menu" )
	concommand.Remove( "-dhlite_menu" )
	
end

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////